<?php
	require_once "../include/User.php";
	require_once "../include/Article.php";
	require_once "../include/functions.php";
	require_once "../include/backend/ArticlesDb.php";

	if ($_GET['act'] == "write" || $_GET['act'] == "edit") {
		if (!hasVal($_POST['subject']) || !hasVal($_POST['text']) 
		    || !isset($_FILES['img'])) {
			header("Location: ../write.php?act=write&err=empty");
			die();
		}
		
		if(!eregi('image/', $_FILES['img']['type'])) {
			header("Location: ../write.php?act=write&err=notimage");
			die();
		}
		
		session_start();
		
		$art['subject'] = makeDbSafe(trim($_POST['subject']));
		$art['text'] = makeDbSafe(trim($_POST['text']));
		$imgName = strtolower($_SESSION['user']->name)."-".date("U");
		$art['image'] = $imgName;
		$art['username'] = strtolower($_SESSION['user']->name);
		$art['latitude'] = $_SESSION['user']->latitude;
		$art['longtitude'] = $_SESSION['user']->longtitude;
		$art['aid'] = $_GET['id'];
		$article = new Article($art);
		
		$imgDir = "../img/pub/";
		if (move_uploaded_file($_FILES['img']['tmp_name'], 
		                       $imgDir.$_FILES['img']['name'])) {
			$img=loadImg($imgDir.$_FILES['img']['name'],$_FILES['img']['type']);
			imagejpeg($img, $imgDir.$imgName.".jpg", 20);
			unlink($imgDir.$_FILES['img']['name']);
			saveThumb($imgDir.$imgName.".jpg", $imgDir.$imgName."s", 150);
			
			$articles = new ArticlesDb();
			if ($_GET['act'] == "write")
				$articles->add($article);
			else if ($_GET['act'] == "edit" && is_numeric($art['aid'])) {
				$old_art = $articles->get(1, $art['aid']);
				$old_art = $articles->result2array($old_art);
				$articles->delArtImg($old_art[0]['image']);
				$article->date = $old_art[0]['date'];
				$articles->update($article);
				header("Location: ../article.php?id=".$art->aid);
			}
		}
		header("Location: ../");
		die();
	}
	else if ($_GET['act'] == "del" && is_numeric($_GET['id'])) {
		$articles = new ArticlesDb();
		$articles->delete(new Article(array('aid' => $_GET['id'])));
		header("Location: ../user.php?u=".$_SESSION['user']->name."&stat=artdel");
	}
	else
		header("Location: ../");

/* Below several functions are defined. They are defined here, because they
 * won't be used elsewhere
 * Also, it only resizes based on the width of the image, since with websites
 * height doesn't matter (you can scroll)
 */

// $loc should be without extension. Also, jpeg image is expected

function saveThumb($imgLoc, $thumbLoc, $newWidth) {
	$img = loadImg($imgLoc, "image/jpeg");
	
	$size = getimagesize($imgLoc);
	// In $size, 0 is width, 1 is height
	$ratio = $newWidth/$size[0];
	if ($ratio < 1) {
		$newSize[0] = floor($ratio*$size[0]);
		$newSize[1] = floor($ratio*$size[1]);
		$thumb = imagecreatetruecolor($newSize[0], $newSize[1]);
		imagecopyresized($thumb, $img, 0, 0, 0, 0, $newSize[0], $newSize[1], 
		                 $size[0], $size[1]);
		imagejpeg($thumb, $thumbLoc.".jpg", 20);
	}
	else
		imagejpeg($img, $thumbLoc.".jpg", 20);
}

function loadImg($imgLoc, $type) {
	if      ($type == "image/pjpeg" || $type == "image/jpeg")
		$img = imagecreatefromjpeg($imgLoc);
	else if ($type == "image/x-png" || $type == "image/png")
		$img = imagecreatefrompng($imgLoc);
	else if ($type == "image/gif")
		$img = imagecreatefromgif($imgLoc);
	
	return $img;
}
?>
